package com.jiahui.vote.controller;

import com.alibaba.fastjson.JSONObject;
import com.jiahui.vote.service.VoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.jiahui.vote.entity.Vote;

@RestController
public class VoteController {
    @Autowired
    VoteService voteService;

    @GetMapping("/vote")
    public JSONObject getAll(){
        Vote[] votes= voteService.getAllVotes();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("object",votes);
        return jsonObject;
    }

    @GetMapping("/vote/{id}")
    public JSONObject getOne(@PathVariable("id") int id){
        Vote vote= voteService.getVoteById(id);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("object",vote);
        return jsonObject;
    }

    @PostMapping("/vote/add")
    public JSONObject add(@RequestBody Vote vote){
        boolean result=voteService.addVote(vote);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("isok",result);
        return jsonObject;
    }

    @DeleteMapping("/vote/delete/{id}")
    public JSONObject add(@PathVariable("id") int id){
        boolean result=voteService.deleteVote(id);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("isok",result);
        return jsonObject;
    }

    @PostMapping("/vote/{id}/{which}")
    public JSONObject vote(@PathVariable("id") int id,@PathVariable("which") int which){
        boolean result=voteService.vote(id,which);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("isok",result);
        return jsonObject;
    }

}
